﻿using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DianShang.Controllers
{
    public class GeRenController : Controller
    {
        // GET: GeRen
        /// <summary>
        /// 个人中心默认进入订单
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            using ( ShopContext db = new ShopContext())
            {
                if (Session["login"] == null)
                {
                    TempData["msg"] = "老弟，你需要先登录";
                    return Redirect("/Home/Login");
                }
                else
                {
                    var l = ((Customer)Session["login"]).Id;
                    ViewBag.name = ((Customer)Session["login"]).CustomerName;
                    var list = db.V_Order.Where(p => p.CustomerId == l).ToList();
                    return View(list);
                }
            }
                
        }
        /// <summary>
        /// 个人信息
        /// </summary>
        /// <returns></returns>
        public ActionResult XinXi()
        {
            using (ShopContext db = new ShopContext()) {
                var lz = ((Customer)Session["login"]).Id;
                ViewBag.touxiang = ((Customer)Session["login"]).CustomerTouXiang;
                ViewBag.name = ((Customer)Session["login"]).CustomerName;
                ViewBag.nicheng = ((Customer)Session["login"]).CustomerNiCheng;
                ViewBag.phone = ((Customer)Session["login"]).CustomerPhone;
                ViewBag.email = ((Customer)Session["login"]).CustomerEmail;
                ViewBag.sex = ((Customer)Session["login"]).CustomerSex;


                return View();
            }
              
        }
        /// <summary>
        /// 收货地址
        /// </summary>
        /// <returns></returns>
        public ActionResult DiZhi()
        {
            using (ShopContext db = new ShopContext())
            {
                var ls = ((Customer)Session["login"]).CustomerName;
                ViewBag.name = ((Customer)Session["login"]).CustomerName;
                var listss = db.Address.Where(p=>p.ShouHuoName==ls).ToList();
                return View(listss);

            }
          
        }
        public ActionResult PubDel(int? Id)
        {
            using (ShopContext db = new ShopContext()) {

              
                    var obj = db.Order.SingleOrDefault(p => p.OrderId == Id);
                    if (obj != null)
                    {
                        db.Order.Remove(obj);
                    }
                    int res = db.SaveChanges();
                    if (res > 0)
                        return Content("删除成功");
                    else
                        return Content("删除失败");
                
                
            }

        }
        public ActionResult PubDels(int? Id)
        {
            using (ShopContext db = new ShopContext())
            {


                var obj = db.Address.SingleOrDefault(p => p.Id == Id);
                if (obj != null)
                {
                    db.Address.Remove(obj);
                }
                int res = db.SaveChanges();
                if (res > 0)
                    return Content("删除成功");
                else
                    return Content("删除失败");


            }

        }
       
    }
}